//
// Created by hjie on 23-7-8.
//
#include <string>
#include <cstdlib>
#include "./../base/link.h"

void ProducerLinkList(LinkList & link_list, int number)
{
    srand(time(NULL));

    link_list.head_node = new LinkNode();
    link_list.tail_node = link_list.head_node;

    link_list.head_node->data = rand() % 50;
    link_list.head_node->next_node = nullptr;
    link_list.head_node->prev_node = nullptr;

    for (int index = 0; index < number - 1; index++)
    {
        int data = rand() % 50;
        LinkNode * node = new LinkNode();
        node->data = data;
        node->next_node = nullptr;
        node->prev_node = nullptr;

        link_list.tail_node->next_node = node;
        node->prev_node = link_list.tail_node;

        link_list.tail_node = node;
    }
    printf("producer data : ");
    LinkNode * ptr = link_list.head_node;
    while (ptr)
    {
        printf(" %d ", ptr->data);
        ptr = ptr->next_node;
    }
    printf("\n");
}

void RemoveElementLinkList()
{
    int number = 20;
    LinkList list{};
    ProducerLinkList(list, number);

    int target_num = 10;
    int length = 0;
    LinkNode * node = list.head_node;
    while(node)
    {
        length++;
        node = node->next_node;
    }
    node = list.head_node;
    int n = length - target_num;
    length = 0;
    LinkNode * prev_node = node;
    while(node)
    {
        if (length == n)
        {
            prev_node->next_node = node->next_node;
            node->next_node->prev_node = prev_node;
            break;
        }
        length++;
        prev_node = node;
        node = node->next_node;
    }
    printf("result data : ");
    LinkNode * ptr = list.head_node;
    while (ptr)
    {
        printf(" %d ", ptr->data);
        ptr = ptr->next_node;
    }
    printf("\n");
}

// use two pointer point this node
// firstly  : first pointer point to the first node prev node (is virtual or fake) and two pointer point to the first node and move to n node
// secondly : move first pointer and two pointer when the second pointer pont to null then we find the target node, it's the first pointer point the node
//
//
void RemoveTargetLinkNode()
{
    srand(time(NULL));
    int number = rand() % 25 + 6;
    LinkList list{};
    ProducerLinkList(list, number);

    int target_num = rand() % number + 1;

    printf("target number : %d.\n", target_num);

    LinkNode * first  = list.head_node;
    LinkNode * second = list.head_node;

    target_num = target_num--;
    while(target_num)
    {
        second = second->next_node;
        target_num--;
    }
    LinkNode * ptr = first;
    while (second != nullptr)
    {
        ptr    = first;
        first  = first->next_node;
        second = second->next_node;
    }
    if (first)
    {
        ptr->next_node = first->next_node;
    }
    printf("result data : ");
    ptr = list.head_node;
    while (ptr)
    {
        printf(" %d ", ptr->data);
        ptr = ptr->next_node;
    }
    printf("\n");
}


int main()
{
    RemoveElementLinkList();
    RemoveTargetLinkNode();
    return 0;
}